package io.xccit.race.controller;

import io.xccit.common.annotation.Log;
import io.xccit.common.core.controller.BaseController;
import io.xccit.common.core.domain.AjaxResult;
import io.xccit.common.core.page.TableDataInfo;
import io.xccit.common.enums.BusinessType;
import io.xccit.race.domain.RaceCompetition;
import io.xccit.race.service.IRaceCompetitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/race/competition")
public class RaceCompetitionController extends BaseController {

    @Autowired
    private IRaceCompetitionService service;

    @PreAuthorize("@ss.hasPermi('race:competition:list')")
    @GetMapping("/list")
    public TableDataInfo list(RaceCompetition query) {
        startPage();
        List<RaceCompetition> list = service.selectRaceCompetitionList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('race:competition:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(service.selectRaceCompetitionById(id));
    }

    @PreAuthorize("@ss.hasPermi('race:competition:add')")
    @Log(title = "竞赛", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RaceCompetition entity) {
        return toAjax(service.insertRaceCompetition(entity));
    }

    @PreAuthorize("@ss.hasPermi('race:competition:edit')")
    @Log(title = "竞赛", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RaceCompetition entity) {
        return toAjax(service.updateRaceCompetition(entity));
    }

    @PreAuthorize("@ss.hasPermi('race:competition:remove')")
    @Log(title = "竞赛", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(service.deleteRaceCompetitionByIds(ids));
    }

    // 前台用户接口（无需权限验证）
    @GetMapping("/available")
    public AjaxResult getAvailableCompetitions() {
        List<RaceCompetition> list = service.selectAvailableCompetitions();
        return success(list);
    }

    @GetMapping("/status/{status}")
    public AjaxResult getCompetitionsByStatus(@PathVariable Integer status) {
        List<RaceCompetition> list = service.selectCompetitionsByStatus(status);
        return success(list);
    }

    @GetMapping("/status/count")
    public AjaxResult getCompetitionStatusCount() {
        List<Map<String, Object>> list = service.selectCompetitionStatusCount();
        return success(list);
    }

    // 统计接口（后台管理）
    @PreAuthorize("@ss.hasPermi('race:competition:list')")
    @GetMapping("/statistics/count")
    public AjaxResult getStatisticsCount() {
        Long count = service.selectCount();
        return success(count);
    }

    @PreAuthorize("@ss.hasPermi('race:competition:list')")
    @GetMapping("/statistics/type-distribution")
    public AjaxResult getTypeDistribution() {
        List<Map<String, Object>> list = service.selectCompetitionTypeDistribution();
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('race:competition:list')")
    @GetMapping("/statistics/time-distribution")
    public AjaxResult getTimeDistribution() {
        List<Map<String, Object>> list = service.selectCompetitionTimeDistribution();
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('race:competition:list')")
    @GetMapping("/statistics/national-distribution")
    public AjaxResult getNationalDistribution() {
        List<Map<String, Object>> list = service.selectNationalCompetitionDistribution();
        return success(list);
    }
}


